package com.example.yuejian2.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.yuejian2.R;
import com.example.yuejian2.bean.IsCollection;
import com.example.yuejian2.common.PictureSelectUtils;
import com.example.yuejian2.view.TitleView;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;
import com.yanzhenjie.permission.PermissionListener;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RationaleListener;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class FBPicActivity extends BaseActivity {

    @BindView(R.id.title_activity)
    TitleView titleView;

    @OnClick(R.id.select)
    void select() {

        if (AndPermission.hasAlwaysDeniedPermission(this, Arrays.asList(Permission.STORAGE))) {
            // 第一种：用默认的提示语。
            PictureSelectUtils.getByAlbum(FBPicActivity.this);
        } else {

            AndPermission.with(this)
                    .permission(Permission.STORAGE)
                    .callback(permissionListener)
                    .rationale(new RationaleListener() {
                        @Override
                        public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
                            // 这里的对话框可以自定义，只要调用rationale.resume()就可以继续申请。
                            AndPermission.rationaleDialog(FBPicActivity.this, rationale).show();
                        }
                    })
                    .start();
        }


    }

    private PermissionListener permissionListener = new PermissionListener() {
        @Override
        public void onSucceed(int requestCode, @NonNull List<String> grantPermissions) {
            Toast.makeText(FBPicActivity.this, "申请成功", Toast.LENGTH_SHORT).show();
            PictureSelectUtils.getByAlbum(FBPicActivity.this);

        }

        @Override
        public void onFailed(int requestCode, @NonNull List<String> deniedPermissions) {
            // 用户否勾选了不再提示并且拒绝了权限，那么提示用户到设置中授权。
            if (AndPermission.hasAlwaysDeniedPermission(FBPicActivity.this, deniedPermissions)) {
                // 第一种：用默认的提示语。
                AndPermission.defaultSettingDialog(FBPicActivity.this, 1).show();
            }
        }
    };

    Handler handler_activity = new mHandler();

    class mHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            IsCollection isCollection = null;

            try {
                isCollection = JSON.parseObject( msg.obj.toString(),IsCollection.class);
            }catch (Exception e){
                ToastUtils.showShort("获取数据失败");
                return;
            }


           if("0".equals(isCollection.getError())){
               ToastUtils.showShort("发布成功");
           }else{
               ToastUtils.showShort("发布失败");
           }

        }
    }

    @OnClick(R.id.fb)
    void fb() {


        EasyHttp.get("yuejian6/public/index.php/json/slideshow/fabu").params("uid", uid).params("intro", textInputEditText.getText().toString()).params("pic", path)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String data) {

                        Message msg = Message.obtain(); // 实例化消息对象
                        msg.what = 0; // 消息标识
                        msg.obj = data; // 消息内容存放

                        handler_activity.sendMessage(msg);
                    }

                    @Override
                    public void onError(ApiException error) {
                        Log.d(TAG, "onFailed: " + error);
                    }
                });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        setContentView(R.layout.activity_fbpic);
        super.onCreate(savedInstanceState);

        initView();
        initData();

    }

    @BindView(R.id.textInputLayout)
    TextInputEditText textInputEditText;
    @BindView(R.id.imageView2)
    ImageView img;
    private Context context;
    private String uid;

    private void initView() {
        uid = SPUtils.getInstance().getInt("uid",0) + "";
        context = getBaseContext();
        titleView.getLeftBackTextTv().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.finishActivity(FBPicActivity.class);
            }
        });
    }

    private void initData() {


    }

    private static final String TAG = "FBPicActivity";
    private String path;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Bitmap bitmap = PictureSelectUtils.onActivityResult(this, requestCode, resultCode, data);
        path = PictureSelectUtils.getPath();
        img.setImageBitmap(bitmap);
    }
}
